package cn.edu.bzu.ie.sf.mealsmisweb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import tk.mybatis.spring.annotation.MapperScan;

import javax.servlet.MultipartConfigElement;
import java.io.File;

@SpringBootApplication
// 下面的这个注解解决 service类中无法自动挂载mapper的问题
@MapperScan(value="cn.edu.bzu.ie.sf.mealsmisweb.mapper")
public class MealsmiswebApplication extends SpringBootServletInitializer {

    /**
     *新增此方法，用于在tomcat部署
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MealsmiswebApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(MealsmiswebApplication.class, args);
    }

    /**
     * 文件上传临时路径，要在程序中指定，否则会使用系统默认的临时路径，但是系统会不定时的清空缓存，导致出现如下错误：
     * java.io.IOException: The temporary upload location [C:\Users\Administrator\AppData\Local\Temp\tomcat.8955432561173472570.8080\work\Tomcat\localhost\ROOT] is not valid
     * 参考地址：https://www.cnblogs.com/grasslucky/p/10614989.html
     * 添加如下的方法，能解决上传临时路径的问题
     * @return
     */
    @Bean
    MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/update/data/tmp";
        File file = new File(location);
        if(!file.exists()){
            file.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
}
